// page/newproject/newproject.js


Page({

    /**
     * 页面的初始数据
     */
    data: {
      baseURL: wx.baseURL,
      inputValue: '',
      indicatorDots: true,
      autoplay: true,
      interval: 3000,
      duration: 1000,
      banners: [],  
      news: [],
      newsIndex: 0,
      newsTimer: 0,
      select1:[],
      recommends: [],
      newItems:'transform: translate(0rpx, 0rpx);transition: transform 0.3s;',
      pageNum: 1,
      total: 0,
      loading: '数据加载中'
    },
  //图片轮播
  getBanners(){
    return new Promise((resolve,reject)=>{
      wx.get('/index/banners').then((res) =>{
        this.setData({
          banners:res
        })
        resolve()
        })
    })
  },
//select1分框四个目录
    getSelect1(){
      return new Promise((resolve,reject)=>{
        wx.get('/index/categorys').then((res) =>{
          this.setData({
            select1:res
          })
          resolve()
          })
      })
    },
    // 获取新闻轮播
    getNews(){////then链式调用一系列请求
      return new Promise((resolve,reject)=>{
        wx.get('/index/news').then((res) =>{
          this.setData({
            news:res
          })
          this.changeNewsIndex()//出发新闻轮播
          resolve()
          })
      })
    },
    //修改新闻轮播
    changeNewsIndex(){
      //清除定时器
      if(this.data.newsTimer){
        clearInterval(this.data.newsTimer)
        this.data.newsTimer = 0
      }
      this.data.newsTimer = setInterval(() =>{
        let newsIndex = this.data.newsIndex
        newsIndex ++
       
        this.setData({
          newsIndex,
          newItems: 'transform: translate(0rpx, -' + newsIndex * 80 +'rpx);transition: transform 0.3s;'
        })
        //延迟代码
        if(newsIndex == 2){
          setTimeout(()=>{
            this.setData({
              newsIndex: 0,
              newItems: 'transform: translate(0rpx, 0rpx);'
            })
          },500)
        }
        this.changeNewsIndex()      
        if(newsIndex == this.data.news.length) newsIndex = 0
      },3000)
    },
    //流畅滑动最后一轮
    endanimation(){
      if(this.data.newsIndex == 3){
        
          this.setData({
            newsIndex:0,
            newItems:'transform: translate(0rpx, 0rpx)'
          })
        
      }
    },
    //////下拉刷新
    onReachBottom(){
      if(this.data.recommends.length < this.data.total){
        this.data.pageNum ++
        this.getRecommends()
      }
      // console.log(this.data.pageNum)


    },
    /////推荐动态获取数据
    getRecommends(){
      return new Promise((resolve, reject) => {
        this.setData({loading:'数据加载中。。。'})

        wx.get('/index/recommends',{
          pageNum: this.data.pageNum
        }).then((res) => {
          this.setData({
            loading:'已经到底了！',
            recommends: this.data.recommends.concat(res.rows),
            total: res.total
          })
          resolve()
        })
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    //下拉刷新    、、、、未实现
    onPullDownRefresh(){
      this.data.pageNum = 1
      this.data.recommends = []
      this.getRecommends()
      this.getNews(()=>{
        console.log('回调成功')
        wx.stopPullDownRefresh()
      })

    },
    onLoad(query) {
      Promise.all([this.getBanners(), this.getNews(), this.getSelect1(), this.getRecommends()]).then(() =>{
        console.log('所有事件加载成功')
      }).catch(e => {
        console.log(e)
      })
        this.changeNewsIndex()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        // console.log('I am onready')
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        // console.log('I am page onshow')
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */

    /**
     * 页面上拉触底事件的处理函数
     */

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
//点击跳转页面
  tapName(event){
    console.log('点击成功',event)
    let path = '/page/onebanner/onebanner'
    wx.showModal({
      cancelColor: 'cancelColor',
      title:'提示',
      content: path,

      success(){
        console.log(path)
        wx.navigateTo({
          url:path,
        })
      }
    })
  }
})

